package cn.knightzz.controller;


import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysBuilding;
import cn.knightzz.service.SysBuildingService;
import cn.knightzz.vo.BuildingVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-05-09
 */
@RestController
public class SysBuildingController {

    @Resource
    SysBuildingService buildingService;

    @PostMapping("/building")
    public JsonResult<SysBuilding> addBuilding(@RequestBody BuildingVO buildingVO){

        Boolean status = buildingService.addBuilding(buildingVO);
        if(status){
            return new JsonResult<>("200" , "添加成功");
        }else{
            return new JsonResult<>("201", "添加失败" );
        }
    }

    @PutMapping("/building")
    public JsonResult<SysBuilding> editBuilding(@RequestBody BuildingVO buildingVO){

        Boolean status = buildingService.updateBuilding(buildingVO);
        if(status){
            return new JsonResult<>("200" , "更新成功");
        }else{
            return new JsonResult<>("201", "更新失败" );
        }
    }

    @DeleteMapping("/building/{id}")
    public JsonResult<SysBuilding> deleteBuilding(@PathVariable Long id){

        Boolean status = buildingService.deleteBuilding(id);
        if(status){
            return new JsonResult<>("200" , "删除成功");
        }else{
            return new JsonResult<>("201", "删除失败" );
        }
    }

    // 分页查询
    @GetMapping("/building/{id}")
    public JsonResult<HashMap<String, Object>> selectByPage(
                                                            @PathVariable("id") Long userId,
                                                            @RequestParam Long pageNum,
                                                            @RequestParam Long pageSize,
                                                            @RequestParam String query){
        IPage<SysBuilding> pages = buildingService.selectByPage(userId,pageNum, pageSize, query);
        long total = pages.getTotal();
        List<SysBuilding> records = pages.getRecords();

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("building", records);

        JsonResult<HashMap<String, Object>> result = new JsonResult<>("200" , "查询成功!");
        result.setData(data);
        return result;
    }

    @GetMapping("/building/area")
    public JsonResult<List<SysBuilding>> getBuildingByArea(@RequestParam("area") String area,
                                                     @RequestParam("query") String query){
        List<SysBuilding> buildingByAreaLikeAddress = buildingService.getBuildingByAreaLikeAddress(area, query);

        JsonResult<List<SysBuilding>> result = new JsonResult<>("200" , "查询成功");
        result.setData(buildingByAreaLikeAddress);
        return result;
    }

}

